﻿using StudySystem.Windows;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudySystem
{
    ///// <summary>
    ///// 用来调节各个控件的visible
    ///// </summary>
    //enum ShowControl
    //{
    //    HIDE,
    //    SHOW
    //};
    #region 不用的方法 要删除
    ///// <summary>
    ///// 将内存中DT数据与课程栏相关联
    ///// 同时将需要显示的组件的text加载出来
    ///// </summary>
    ///// <param name="subjects"></param>
    ///// <param name="dt"></param>
    ///// <param name="key"></param>
    //public static void SubjectLinkDB(List<Subject> subjects, DataTable dt, string key)
    //{
    //    for(int i = 0;i<dt.Rows.Count;i++)
    //    {
    //        Subject s = new Subject(dt.Rows[i]);
    //        //s.Tag = dt.Rows[i][key];

    //        //设置组件的text
    //        SetSubjectContent(s, dt.Rows[i]["SubjectName"].ToString(), dt.Rows[i]["QuesCount"].ToString());
    //        subjects.Add(s);
    //    }
    //}

    //private static void SetSubjectContent(Subject s, string subjectName, string quesCount)
    //{
    //    s.SetSubjectName(subjectName);
    //    s.SetSubjectCount(quesCount);
    //    s.SetLblCount("0");
    //    TrackBar tr = s.GetTrackBar();
    //    tr.Maximum = Convert.ToInt32(quesCount);
    //}
    #endregion


    /**
     *主要用来被Logic类等调用
     * 将一些主要界面保存在内存中
     * */
    static class FormController
    {
        //登录界面，同时也是主界面
        public static Form mainForm;
        
        //课程界面
        public static FormSubject subjectForm;

        //问题界面
        //public static FormQuestions questionForm;
        
        /// <summary>
        /// 打开课程界面
        /// </summary>
        public static void OpenSubjectForm()
        {
            FormSubject formSubject = new FormSubject();
            //Application.Run(formMain);
            subjectForm = formSubject;
            formSubject.Show();
        }
        
        ///// <summary>
        ///// 打开问题界面
        ///// </summary>
        ///// <param name="subjectId"></param>
        //public static void OpenQuestionForm(DataRow subject)
        //{
        //    FormQuestions formQuestion = new FormQuestions(subject);
        //    //Application.Run(formMain);
        //    //questionForm = formQuestion;

        //    //标识当前问题，load事件根据此标记加载数据
        //    //formQuestion.Tag = subjectId;
        //    formQuestion.Show();
        //}

       

        /// <summary>
        /// 将与dt关联好的课程放到指定容器中
        /// </summary>
        /// <param name="subjects"></param>
        /// <param name="address"></param>
         public static void LoadSubject(List<Subject> subjects,FlowLayoutPanel address)
        {
            foreach(var subject in subjects)
            {
                address.Controls.Add(subject);
            }
        }

        ///// <summary>
        ///// 将内存中的问题与dt联系起来
        ///// 将要显示的内容加载到text
        ///// </summary>
        ///// <param name="questions"></param>
        ///// <param name="dt"></param>
        ///// <param name="key"></param>
        //public static void QuestionLinkDB(string subjectId,List<Question> questions, DataTable dt, string key)
        //{
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        if(dt.Rows[i]["SubjectID"].ToString() == subjectId)
        //        {
        //            Question q = new Question(dt.Rows[i]);
        //            //q.Tag = dt.Rows[i][key];

        //            //加载text
        //            q.SetQuestionRichTextBox(dt.Rows[i]["QuestionContent"].ToString());
        //            q.SetAnswerRichTextBox(dt.Rows[i]["Answer"].ToString());
                   

        //            questions.Add(q);
        //        }
                
        //    }
        //}

        /// <summary>
        /// 将内存中的问题组件组放到指定容器中
        /// </summary>
        /// <param name="questions"></param>
        /// <param name="address"></param>
        public static void LoadQuestion(List<Question> questions, FlowLayoutPanel address)
        {
            
            foreach (var question in questions)
            {
                address.Controls.Add(question);
            }
        }


        public static void ConfigCombobox(ComboBox cbb, object []items, string displayMember)
        {
            cbb.Items.Clear();
            cbb.BeginUpdate();
            cbb.Items.Add("全部学科");
            cbb.Items.AddRange(items);
            cbb.EndUpdate();
            cbb.DisplayMember = displayMember;
            cbb.SelectedIndex = 0;
        }

        /// <summary>
        /// 删除flp中的所有元素
        /// </summary>
        /// <param name="flp"></param>
        public static void DeleteElement(FlowLayoutPanel flp)
        {
            for (int i = 0;flp.Controls.Count > 0;)
            {
                flp.Controls[i].Dispose();
            }
        }


        /// <summary>
        /// 双击隐藏指定RichTextBox的内容
        /// </summary>
        /// <param name="answerState"></param>
        /// <param name="rtb"></param>
        /// <param name="showTxt"></param>
        public static void TxtShowControl(AnswerState answerState, RichTextBox rtb, string showTxt)
        {
            if(answerState == AnswerState.Show)
            {
                rtb.Text = showTxt;
                rtb.ForeColor = Color.Black;
            }
            else if(answerState == AnswerState.Hide)
            {
                rtb.Text = "当前内容以隐藏，双击显示！";
                rtb.ForeColor = Color.Red;
            }
        }

        /// <summary>
        /// 检查问题是否为空
        /// </summary>
        /// <param name="question"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public static bool CheckQuestion(string question, string answer)
        {
            if (question != "" || answer != "")
            {
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 显示或隐藏选择问题相关组件
        /// </summary>
        /// <param name="flpSub"></param>
        /// <param name="showOrHide"></param>
        /// <param name="controlBtn"></param>
        public static void SelectQuestionControl(FlowLayoutPanel flpSub,GroupBox showOrHide, Button controlBtn)
        {
            foreach (Subject s in flpSub.Controls)
            {
                s.GetQuesControl().Visible = !s.GetQuesControl().Visible;
            }
            showOrHide.Visible = !showOrHide.Visible;
            if(controlBtn.Text == "随机出题")
            {
                controlBtn.Text = "取消出题";
            }
            else
            {
                controlBtn.Text = "随机出题";
            }
        }

        /// <summary>
        /// 设置TrackBar的值
        /// </summary>
        /// <param name="num"></param>
        /// <param name="flp"></param>
        public static void SetAllSubQuesCount(int num, FlowLayoutPanel flp)
        {
            foreach (Subject s in flp.Controls)
            {
                TrackBar tr = s.GetTrackBar();
                tr.Value = tr.Maximum>=num?num:tr.Maximum;
            }
        }


    }
}
